package com.wlyuan.open.constants;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lijing
 */
@Getter
public enum VolumeUnit {

    CBM("立方米", 3),
    LTR("升", 4),
    ;

    private String name;
    private Integer value;

    VolumeUnit(String name, Integer value) {
        this.value = value;
        this.name = name;
    }

    private static final Map<Integer, VolumeUnit> MAP = Arrays.asList(VolumeUnit.values()).stream().collect(Collectors.toMap(VolumeUnit::getValue, Function.identity()));

    public static VolumeUnit fromValue(Integer value) {
        return Optional.ofNullable(value).map(a -> Optional.ofNullable(MAP.get(a)).orElse(LTR)).orElse(LTR);
    }

}
